package pers.lan.hash;

import java.util.HashSet;
import java.util.Random;
import java.util.Set;

/**
 * @author <p><a href="mailto:1728209643@qq.com">XiaoSan</a>
 * @date 2019/11/25 8:40 AM
 */
public class LC_380 {

    public static void main(String[] args) {
        RandomizedSet set = new RandomizedSet();
        System.out.println(set.insert(2));
        System.out.println(set.insert(2));
        System.out.println(set.insert(1));
        System.out.println(set.insert(13));
        System.out.println(set.insert(15));
        System.out.println(set.remove(15) );
        System.out.println(set.remove(15) );
        System.out.println("====");
        for (int i = 0; i < 10; i++) {
            System.out.println(set.getRandom());
        }
    }

}

class RandomizedSet {

    Set<Integer> set;
    Random random;

    /**
     * Initialize your data structure here.
     */
    public RandomizedSet() {
        set = new HashSet<>();
        random = new Random();
    }

    /**
     * Inserts a value to the set. Returns true if the set did not already contain the specified element.
     */
    public boolean insert(int val) {
        return set.add(val);
    }

    /**
     * Removes a value from the set. Returns true if the set contained the specified element.
     */
    public boolean remove(int val) {
        return set.remove(val);
    }

    /**
     * Get a random element from the set.
     */
    public int getRandom() {
        return (Integer) set.toArray()[random.nextInt(set.size())];
    }
}
